package com.example.config;

import cn.hutool.core.bean.BeanUtil;
import com.example.domain.User;
import com.example.untils.RedisConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class UserIntercetor implements HandlerInterceptor {
    private StringRedisTemplate template;

    public UserIntercetor(StringRedisTemplate template) {
        this.template = template;
    }

    //目标方法执行之前 执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("被拦截了....");
        Boolean flag = template.hasKey(RedisConstants.LOGIN_USER_KEY);
        if(!flag){
            response.sendRedirect("/login.html");
            return false;    //表示不放行
        }
        return true;     //表示放行
    }

}
